var api = require("../../api.js"),
    app = getApp();
  Page({
    data: {
      childTabbarList: {},
      cat_list: [],
      sub_cat_list_scroll_top: 0,
      scrollLeft: 0,
      page: 1,
      cat_style: 0,
      height: 0,
      sortList: [],
      mchId: null,
      currentSort: null
    },
    onLoad: function(options) {
      app.pageOnShow(this)
      app.editTabBar()
      if (app.globalData.mchId) {
        // 切换tabbar时，从 app.js 中获取保存的值
        wx.showLoading({
          title: "正在加载",
          mask: !0
        })
        this.loadSortData(app.globalData.mchId)
      }
    },
    // 加载分类列表
    loadSortData(mchId) {
      var that = this;
      var path = "shop_cat_list_mch_id_" + mchId,
        cacheData = wx.getStorageSync(path);
      cacheData && that.setData({
        sortList: cacheData
      }), wx.showNavigationBarLoading(), app.request({
        url: api.mch.shop_cat,
        data: {
          mch_id: mchId
        },
        success: function(res) {
          0 == res.code && (that.setData({
              sortList: res.data.list
            }),
            that.sortItemClick({currentTarget: {dataset: {index: 0}}}),
            wx.setStorageSync(path, res.data.list))
        },
        complete: function() {
          wx.hideLoading()
          wx.hideNavigationBarLoading()
        }
      })
    },
    sortItemClick(event) {
      const curIndex = event.currentTarget.dataset.index
      const sortList = this.data.sortList
      let curSort = null
      for (let index in sortList) {
        if (parseInt(index) === curIndex) {
          sortList[index].active = 1
          curSort = sortList[index]
        } else {
          sortList[index].active = 0
        }
      }
      this.setData({
        sortList,
        sub_cat_list_scroll_top: 0,
        currentSort: curSort
      })
    }
  });